package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.PointSearchFilter;
import com.example.fireassistant_server.dto.polyline.PolyLineCreateRequest;
import com.example.fireassistant_server.dto.polyline.PolyLineUpdateRequest;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.mapper.PolyLineMapper;
import com.example.fireassistant_server.service.PolyLineService;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import com.example.fireassistant_server.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/line")
public class PolyLineController {

    private WechatUserService wechatUserService;

    private PolyLineMapper polyLineMapper;

    private PolyLineService polyLineService;

    private UserService userService;

    @PostMapping("/add/{user_id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN') ")
    PolyLineVo Add(@Validated @RequestBody PolyLineCreateRequest polyLineCreateRequest, @PathVariable String user_id){
        User user = userService.get(user_id);
        return polyLineMapper.toVo(polyLineService.addLine(polyLineCreateRequest, user));
    }

    // 微信端添加标记线 ；普通用户可以添加
    @PostMapping("/weChat/add")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN') ")
    PolyLineVo weAdd(@Validated @RequestBody PolyLineCreateRequest polyLineCreateRequest, @RequestHeader(value = "X-WX-OPENID") String openId){
        User user = wechatUserService.getCurrentUser(openId).getUser();
        return polyLineMapper.toVo(polyLineService.addLine(polyLineCreateRequest,user));
    }

    //根据id来更新 polyLine
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN') ")
    PolyLineVo update(@PathVariable String id,
                         @Validated @RequestBody PolyLineUpdateRequest polyLineUpdateRequest) {
        return polyLineMapper.toVo(polyLineService.update(id,polyLineUpdateRequest));
    }



    //根据id获取线段
    @GetMapping("/{id}")
    PolyLineVo get(@PathVariable String id) {
        return polyLineMapper.toVo(polyLineService.get(id));
    }

    @PostMapping("/search")
    Page<PolyLineVo> search(@Validated @RequestBody PointSearchFilter pointSearchFilter) {
        return polyLineService.search(pointSearchFilter).map(polyLineMapper::toVo);
    }

    //根据id删除线段
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN') ")
    void delete(@PathVariable String id) {
        polyLineService.delete(id);
    }


    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Autowired
    public void setPolyLineMapper(PolyLineMapper polyLineMapper) {
        this.polyLineMapper = polyLineMapper;
    }

    @Autowired
    public void setPolyLineService(PolyLineService polyLineService) {
        this.polyLineService = polyLineService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
